﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ExaminationTT.Rule;
using ExaminationTT.Control;

namespace ExaminationTT.View
{
    public partial class RulesSetter : Form
    {
        private HostApplication _host;
        private IEnumerable<ViewRule> _rules;
        public RulesSetter(IEnumerable<ViewRule> rules, HostApplication host)
        {
            InitializeComponent();
            _rules = rules;
            _dgv_Rules.AutoGenerateColumns = false; 
            _dgv_Rules.DataSource = rules;
            _host = host;
        }

        private void _dgv_Rules_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (_dgv_Rules.CurrentRow == null) return;
            if (_dgv_Rules.Columns[e.ColumnIndex].Name == _clmn_activateRule.Name)
            {
                DataGridViewCheckBoxCell checkCell =
                    (DataGridViewCheckBoxCell)_dgv_Rules.Rows[e.RowIndex].Cells[_clmn_activateRule.Name];
                bool activate = (bool)checkCell.FormattedValue;
                IRule rule = _dgv_Rules.Rows[e.RowIndex].DataBoundItem as IRule;
                

                _dgv_Rules.Invalidate();
            }
        }

        private void RulesSetter_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (ViewRule vr in _rules)
            {
                if (vr.Active) _host.ActivateRule(vr.Rule);
                else
                {
                    _host.DeactivateRule(vr.Rule);
                }
            }
        }
    
       


    }
}
